<%@page language="java" contentType="text/html; charet=UTF-8" pageEncoding="UTF-8"%>

<%@page import="org.json.JSONObject"%>
<%@page import="com.mtbu.bean.*"%>
<%@page import="com.mtbu.dao.DAOManager"%>
<%@page import="com.mtbu.service.LogUtil"%>

<%@page import="java.util.Date"%>
<%@page import="java.util.Arrays"%>

<%@page import="com.util.SuperString"%>
<%@page import="com.util.SuperDate"%>
<%@page import="com.util.HttpUtil"%>



<%@include file="sys_commcheck.jsp"%>


<%
	//todo 需要加入权限检查
	String act = SuperString.notNullTrim(request.getParameter("act"));
	String ids = SuperString.notNullTrim(request.getParameter("ids"));
	int pid = SuperString.getInt(request.getParameter("pid"));
	System.out.println("act:" + act);
	System.out.println("ids:" + ids);
	System.out.println("pid:" + pid);

	JSONObject json = new JSONObject();
	SuperDate sdnow = new SuperDate();
	//批量添加 从基础库到cntv库，参数是channelid
	if (act.equals("bcadd")) {
		try {
			//找到所属的project
			Project project = DAOManager.projectDao.findProject(pid);
			String[] idarr = ids.split(",");
			for (int i = 0; i < idarr.length; i++) {
				int theid = SuperString.getInt(idarr[i]);
				Channel channel = DAOManager.channelDao.findChannel("channelid",theid,project.getSplit());
				if(channel==null || channel.getId()==0){
					Channel newch = new Channel();
					newch.setChannelId(theid);
					newch.setStepId(project.getChannelStepBegin().getId());
					newch.setStatus(0);
					newch.setCreateTime(sdnow.getDate());
					newch.setUpdateTime(sdnow.getDate());
					DAOManager.channelDao.save(newch, project.getSplit());

				}else{
					//如果已经加入过项目
					Channel modch = new Channel();
					modch.setId(channel.getId());
					modch.setStepId(project.getChannelStepBegin().getId());
					modch.setStatus(0);
					modch.setCreateTime(new Date());
					modch.setUpdateTime(sdnow.getDate());
					DAOManager.channelDao.update(modch, project.getSplit());

				}
			}
			LogUtil.writeLog(Constant_UserName, "加入"+project.getName(),
					ids, request, null);
			json.put("result", "1");
			out.println(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
			json.put("result", "0");
			out.println(json.toString());
		}
	}

	//批量删除 从库里找到记录，参数是id
	if (act.equals("bcdel")) {
		try {
			Project project = DAOManager.projectDao.findProject(pid);
			String[] idarr = ids.split(",");
			for (int i = 0; i < idarr.length; i++) {
				int theid = SuperString.getInt(idarr[i]);

				Channel delch = DAOManager.channelDao.findChannel("id",theid,project.getSplit());
				delch.setId(theid);
				delch.setStatus(-1);
				DAOManager.channelDao.delete(delch, project.getSplit());
			}
			LogUtil.writeLog(Constant_UserName, "删除"+project.getName()+"频道",
					ids, request, null);
			json.put("result", "1");
			out.println(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
			json.put("result", "0");
			out.println(json.toString());
		}
	}
%>